#ifndef SYNCMANAGER_H
#define SYNCMANAGER_H

#include <iostream>
#include <QObject>
#include <QString>
#include <QVector>
#include <sys/stat.h>
#include <dirent.h>
#include <unistd.h>
#include <fcntl.h>
#include <cstring>
#include <QCryptographicHash>
#include <pthread.h>

class SyncManager : public QObject {
    Q_OBJECT

public:
    SyncManager(QObject *parent = nullptr);
    void startSync(const QString &dirA, const QString &dirB, bool resolveConflicts);

signals:
    void progressSyncUpdated(int progress);
    void finished();
    void logMessage(const QString &message);  // 新增日志消息信号


public slots:


private:
    struct FileCopyArgs {
        std::string source;
        std::string target;
        off_t startOffset;
        off_t endOffset;
    };

    void copyFile(const std::string &source, const std::string &target, bool useMultiThread);
    void copyDirectory(const std::string &dirA, const std::string &dirB, bool resolveConflicts);
    int calculateTotalFiles(const std::string &dirPath);
    bool createDirectoryIfNotExists(const std::string &dirPath);
    struct stat getFileStat(const std::string &filePath);
    QByteArray getFileChecksum(const std::string &filePath);
    static void* fileCopyThread(void* arg);
    void emitFinished();

    int totalFiles;
    int copiedFiles;
    int maxThreads; // 最大线程数
};

#endif // SYNCMANAGER_H
